<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title data-culture="category">栏目维护</title>
</head>
<body>
<div class="container-fluid">
    <div class="row">
        <div class="col-lg-3 col-sm-4 col-xs-12">
            <div class="widget no-padding">
                <div class="widget-header bg-themeprimary ">
                    <i class="widget-icon typcn typcn-th-large-outline"></i>
                    <span class="widget-caption" data-culture="basedata">栏目维护</span>
                </div>
                <div class="widget-body">
                    <ul id="dicTree" class="ztree" style="width: auto; overflow: auto;"></ul>
                </div>
            </div>
        </div>
        <div class="col-lg-9 col-sm-8 col-xs-12">
            <div class="widget  no-padding">
                <div class="widget-header bg-themeprimary">
                    <i class="widget-icon typcn typcn-th-list"></i>
                    <span class="widget-caption" data-culture="category">栏目维护</span>
                </div>
                <div class="widget-body">
                    <table id="grid-data">
                        <thead>
                        <tr>
                            <th style="text-align: center;">ID</th>
                            <th style="text-align: center;" data-culture="name">名称</th>
                            <th style="text-align: center;" data-culture="parent">上级</th>
                            <th style="text-align: center;" data-culture="sort">序号</th>
                            <th style="width: 200px; text-align: center;" data-culture="operation">操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr class="row-no-display">
                            <td style="text-align: center;">$id$</td>
                            <td style="text-align: center;">$name$</td>
                            <td style="text-align: center;">$parentName$</td>
                            <td style="text-align: center;">$sort$</td>
                            <td class="text-left" style="text-align: center;">
                                <a rel='editDic' class="btn btn-info btn-xs edit"><i
                                        class="fa fa-edit"></i> <span data-culture="edit">编辑</span></a>
                                <!--<a rel='deleteDic' class="btn btn-danger btn-xs edit"><i-->
                                <!--class="fa fa-times"></i> <span data-culture="delete">删除</span></a>-->
                            </td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade form" id="editmodal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
     aria-hidden="true">
    <div class="modal-dialog">
        <form class="form-horizontal form-bordered" role="form" id="editForm">
            <div class="widget no-padding">
                <div class="widget-header bg-themeprimary ">
                    <i class="widget-icon glyphicon glyphicon-credit-card"></i>
                    <span class="widget-caption" data-culture="categorymgmt">栏目管理</span>
                    <div class="widget-buttons">
                        <a tabindex="7" data-type="reset"><i class="fa fa-refresh"></i></a>
                        <a data-dismiss="modal">
                            <i class="fa fa-times"></i>
                        </a>
                    </div>
                </div>
                <div class="widget-body">
                    <table class="table table-bordered form">
                        <tbody>
                        <tr>
                            <th><label for="name">栏目名称</label></th>
                            <td>
                                <input type="text" class="form-control" id="name" name="name" data-culture="categorymgmt"
                                       placeholder="数据项名称">
                                <input type="hidden" id="id" name="id">
                            </td>
                        </tr>
                        <tr>
                            <th>
                                <label for="parentName" data-culture="parent">上级</label>
                            </th>
                            <td>
                                <input type="text" class="form-control" disabled="disabled"
                                       style="background: #EEEEE0" id="parentName" name="parentName"/>
                                <input type="hidden" id="parentId" name="parentId">
                            </td>
                        </tr>
                        <tr>
                            <th>
                                <label for="sort">排序</label>
                            </th>
                            <td><input type="text" class="form-control" id="sort" name="sort" placeholder="排序">
                            </td>
                        </tr>
                        </tbody>
                    </table>
                </div>
                <div class="modal-footer">
                    <button id="btnSave" type="submit" class="btn btn-success" data-loading-text="保存中..."
                            data-culture="save">保存
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<script type="text/javascript">
    var categoryObj = {
        insert: true,
        dicTreeObj: null,
        currentDic: null,
        currentTreeNode: null,
        tempTreeNode: null,
        validateParam: {
            submitHandler: function (obj) {
                obj.insert = categoryObj.insert;
                if (!categoryObj.insert) {
                    obj.id = categoryObj.currentDic.id;
                }
                categoryObj.tempTreeNode = categoryObj.currentTreeNode;
                obj.parentIds = "";
                while (categoryObj.tempTreeNode.getParentNode()) {
                    obj.parentIds += categoryObj.tempTreeNode.id + ',';
                    categoryObj.tempTreeNode = categoryObj.tempTreeNode.getParentNode();
                }
                obj.parentIds += categoryObj.tempTreeNode.id;
                $('#btnSave').button('loading');
                $.request("/cms/category/save", obj, function (result) {
                    $('#editmodal').modal('hide');
                    initdic();
                    categoryObj.grid.ext.refresh();
                    bootbox.alert('保存成功！');
                }, function () {
                    bootbox.alert('保存失败！');
                }).always(function () {
                    $('#btnSave').button('reset');
                    categoryObj.validateParam.resetBtn.trigger("reset");
                });
            },
            fields: {
                name: {
                    validators: {
                        notEmpty: {}
                    }
                },
                sort: {
                    validators: {
                        notEmpty: {},
                        dataType: {
                            dataType: 'decimal',
                            message: '只能输入数字'
                        }
                    }
                }
            }
        },
        grid: {
            keys: ['name'],
            url: "/cms/category/query/page/parentId",
            placeholder: '数据项名称',
            stableparm: function () {
                return {parentId: categoryObj.currentTreeNode.id};
            },
            events: {
                editDic: function (row) {
                    categoryObj.insert = false;
                    categoryObj.currentDic = row;
                    categoryObj.validateParam.resetBtn.trigger("reset");
                    $("#parentName").val(categoryObj.currentTreeNode.name);
                    $.setobject(row, 'editForm');
                    $('#editmodal').modal('show');
                },
                deleteDic: function (row) {
                    bootbox.confirm('确认删除？', function () {
                        deleteRowList([id = row.id]);
                    });
                }
            },
            tools: [{
                text: '新增', 'data-culture': 'add', img: 'fa fa-plus', event: function (selectItems) {
                    categoryObj.insert = true;
                    categoryObj.validateParam.resetBtn.trigger("reset");
                    if (categoryObj.currentTreeNode != undefined || categoryObj.currentTreeNode != null) {
                        $("#parentName").removeAttr("readOnly");
                        $("#id").val('0');
                        $("#parentName").val(categoryObj.currentTreeNode.name);
                        $("#parentId").val(categoryObj.currentTreeNode.id);
                        $('#name').val('');
                        $('#enName').val('');
                        $('#sort').val('');
                    }
                    $('#editmodal').modal('show');
                }
            }, {
                text: '编辑', 'data-culture': 'edit', img: 'glyphicon glyphicon-edit', event: function (selectItems) {
                    if (selectItems && selectItems.length == 1) {
                        categoryObj.insert = false;
                        categoryObj.validateParam.resetBtn.trigger("reset");
                        updateRow(selectItems[0]);
                    }
                    else {
                        bootbox.alert('请选择一条记录！');
                    }
                }
            }
                , {
                text: '删除', 'data-culture': 'delete', img: 'glyphicon glyphicon-remove', event: function (selectItems) {
                    if (selectItems && selectItems.length > 0) {
                        bootbox.confirm('确认删除？', function () {
                            var ids = [];
                            $.each(selectItems, function (i, row) {
                                ids.push(row.id);
                            });
                            deleteRowList(ids);
                        });
                    } else {
                        bootbox.alert('请选择待删除的记录！');
                    }
                }
            }
            ]
        }
    }
    function initdic() {
        categoryObj.dicTreeObj = $('#dicTree').tree({
            data: {
                key: {name: 'name'},
                simpleData: {
                    enable: true,
                    idKey: "id",
                    pIdKey: "parentId",
                    rootPId: null,
                    url: '/cms/category/query/all',
                }
            },
            callback: {
                onClick: onClick,
                beforeClick: function () {
                }
            }
        });
        if (categoryObj.currentTreeNode) {
            categoryObj.dicTreeObj.selectNode(categoryObj.currentTreeNode = categoryObj.dicTreeObj.getNodeByParam('id', categoryObj.currentTreeNode.id));
        } else {
            categoryObj.dicTreeObj.selectNode(categoryObj.currentTreeNode = categoryObj.dicTreeObj.getNodes()[0]);
        }
    }
    function onClick(event, treeId, treeNode, clickFlag) {
        categoryObj.insert = false;
        categoryObj.currentTreeNode = treeNode;
        categoryObj.grid.ext.refresh();
    }
    function updateRow(obj) {
        categoryObj.currentDic = obj;
        obj.parentId = categoryObj.currentTreeNode.id;
        obj.parentName = categoryObj.currentTreeNode.name;
        $.setobject(obj, 'editForm');
        $('#editmodal').modal('show');
    }
    //    function deleteRowList(ids) {
    //        $.request('/cms/category/delete/ids', {ids: ids}, function () {
    //            categoryObj.grid.ext.refresh();
    //            initdic();
    //            bootbox.alert("删除成功!");
    //        }, function () {
    //            bootbox.alert("删除失败!");
    //        })
    //    }
    function init() {
        initdic();
        $("#grid-data").table(categoryObj.grid);
        $('#editForm').validate(categoryObj.validateParam);
    }
    function dispose() {
        categoryObj = {};
    }
</script>
</body>
</html>